// app/service/news.js
const querystring = require('querystring')
const Service = require('egg').Service;

class NewsService extends Service {
    //获取新闻详细内容
    async getNewsDetail(id = 1) {
        const newsDetailUrl = this.config.news.newsDetailUrl
        const newsData = await this.ctx.curl(newsDetailUrl + id, {
            dataType: 'json'
        })
        return newsData.data
    }
    //按分类获取新闻列表
    async getCatNewsList(catId = 0, page = 1) {
        const getCatNewsUrl = this.config.news.getcatnews
        const data = await this.ctx.curl(`${getCatNewsUrl}${catId}?page=${page}`, {
            dataType: 'json'
        })
        return data.data
    }
    //按关键词获取新闻列表
    async getSearchNews(keyword = "南宁", page = 1) {
        const getSearchNewsUrl = this.config.news.searchNewsUrl
        const querykeyword = querystring.escape(keyword)
        console.log(querykeyword)
        const data = await this.ctx.curl(`${getSearchNewsUrl}?page=${page}&keyword=${querykeyword}`,{
            dataType: 'json'
        })
        return data.data
    }
}

module.exports = NewsService